#include <thread>
#include <iostream>
#include <mutex>

using std::cout;
using std::endl;

std::mutex g_lock;

void func()
{
    cout << "\n" << " get lock" << std::this_thread::get_id() << endl;
    g_lock.lock();
    cout << "entred thread " << std::this_thread::get_id() << endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    cout << "Leaving thread " << std::this_thread::get_id() << endl;

    //g_lock.unlock();
}

void func2()
{
    std::lock_guard<std::mutex> locket(g_lock);
    cout << "\n";
    cout << "entred thread " << std::this_thread::get_id() << endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    cout << "Leaving thread " << std::this_thread::get_id() << endl;
}

int main(void)
{
    std::thread t1(func2);
    std::thread t2(func2);
    std::thread t3(func2);

    t1.join();
    t2.join();
    t3.join();

    return 0;
}
